package com.zzyl.nursing.controller;

import com.huaweicloud.sdk.iotda.v5.model.ServiceCapability;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.DeviceReportDataVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备表Controller
 * 
 * @author zbr
 * @date 2025-03-05
 */
@Api(tags = "设备表管理")
@RestController
@RequestMapping("/nursing/device")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备表列表
     */
    @ApiOperation("查询设备表列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    public TableDataInfo<List<Device>> list(@ApiParam("设备表查询条件") Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 物联网平台同步产品列表
     * @return
     */
    @ApiOperation("同步产品列表")
    @PostMapping("/syncProductList")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return success();
    }

    /**
     * 查询IOT产品列表
     * @return
     */
    @GetMapping("/allProduct")
    @ApiOperation(value = "查询所有产品列表")
    public R<List<ProductVo>> allProduct() {
        return R.ok(deviceService.getAllProduct());
    }

    /**
     * 注册设备
     *
     * @return
     */
    @ApiOperation("注册设备")
    @PostMapping("/register")
    public AjaxResult register(@RequestBody DeviceDto dto) {
        deviceService.register(dto);
        return success();
    }

    /**
     * 删除设备
     *
     * @param iotId 设备ID
     * @return 结果
     */
    @ApiOperation("删除设备")
    @DeleteMapping("/{iotId}")
    public AjaxResult deleteByIotId(@PathVariable String iotId) {
        deviceService.deleteByIotId(iotId);
        return success();
    }

    /**
     * 查看设备详细信息
     *
     * @param iotId 设备ID
     * @return 结果
     */
    @ApiOperation("查看设备详细信息")
    @GetMapping("/{iotId}")
    public R<DeviceDetailVo> getByIotId(@PathVariable String iotId) {
        DeviceDetailVo vo = deviceService.getByIotId(iotId);
        return R.ok(vo);
    }

    /**
     * 查询设备上报的数据
     * @return
     */
    @ApiOperation("查询设备上报的数据")
    @GetMapping("/queryServiceProperties/{iotId}")
    public R<List<DeviceReportDataVo>> queryServiceProperties(@PathVariable String iotId) {
        List<DeviceReportDataVo> list = deviceService.queryServiceProperties(iotId);
        return R.ok(list);
    }


    @ApiOperation("修改设备")
    @PutMapping
    public AjaxResult updateDevice(@RequestBody Device device) {
        deviceService.updateDeviceById(device);
        return success();
    }

    /**
     * 查询产品详情
     * @return productKey
     */
    @ApiOperation("查询产品详情")
    @GetMapping("/queryProduct/{productKey}")
    public R<List<ServiceCapability>> queryProduct(@PathVariable String productKey) {
        List<ServiceCapability> list = deviceService.queryProduct(productKey);
        return R.ok(list);
    }

}
